<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>词占比</title>
    <style>
        body{
            background: skyblue;
        }
    </style>
</head>
<body>
    
</body>
</html>
<?php
    // 打开文件
    $keyFile = fopen('./upload/key.txt', 'r+');
    $sentenceFile = fopen('./upload/sentence.txt', 'r+');
    $keywords = fread($keyFile,filesize("./upload/key.txt"));
    $sentences = fread($sentenceFile,filesize("./upload/sentence.txt"));

    $parrten = "/[a-zA-Z]+/";

 
    $sentences = strtolower($sentences);
    $keywords = strtolower($keywords);
    $count = 0;
    $tmpKey = '';
    $tmpSen = '';
    // 关键字总数
    preg_match_all($parrten, $keywords, $keywordsArr, PREG_SET_ORDER);
    // echo '关键词总数:'.count($keywordsArr).'<br />';
    for ($i=0; $i<count($keywordsArr); $i++) {
        if ($i == count($keywordsArr)-1) {
            $tmpKey .= $keywordsArr[$i][0];
        } else {
            $tmpKey .= $keywordsArr[$i][0].",";
        }
    }

    $keywordsArr =  explode(",", $tmpKey);
    // var_dump($keywordsArr);
    // 所有句子总单词
    preg_match_all($parrten, $sentences, $sentencesArr, PREG_SET_ORDER);
    echo '<h2 style="text-align:center;">所有句子中单词总数: <span style="color:green;">'.count($sentencesArr).'</span></h2>';
    for ($i=0; $i<count($sentencesArr); $i++) {
        if ($i == count($sentencesArr)-1) {
            $tmpSen .= $sentencesArr[$i][0];
        } else {
            $tmpSen .= $sentencesArr[$i][0].",";
        }
    }

    $sentencesArr =  explode(",", $tmpSen);
    // var_dump($sentencesArr);

    // var_dump(array_count_values($sentencesArr));
    echo '<table style="margin: 0 auto;" width=600 border=2>';
    echo '<th>序号</th>';
    echo '<th>关键词</th>';
    echo '<th>出现次数</th>';
    
    for($j=0; $j<count($keywordsArr); $j++){
        if(in_array($keywordsArr[$j], $sentencesArr)){
            $count += array_count_values($sentencesArr)[$keywordsArr[$j]];
        }
    }
    $rate = round($count/(count($sentencesArr)),4)*100;
    echo '<tr><td style="text-align:center;"></td><td  style="text-align:center;color:red;">关键词出现总次数</td><td  style="text-align:center;color:green;">'.$count.'</td><tr>';
    echo '<tr><td style="text-align:center;"></td><td  style="text-align:center;color:red;">关键词占总词数</td><td  style="text-align:center;color:green;">'.$rate.'%</td><tr>';
    for($j=0; $j<count($keywordsArr); $j++){
        echo '<tr>';

        if(in_array($keywordsArr[$j], $sentencesArr)){
          echo '<td style="text-align:center;">'.($j+1).'</td>';
          echo '<td style="text-align:center;">'.$keywordsArr[$j].'</td>';
          echo '<td style="text-align:center;color:green;">'.array_count_values($sentencesArr)[$keywordsArr[$j]].'</td>';
            // $count += array_count_values($sentencesArr)[$keywordsArr[$j]];
        }

        echo '</tr>';
    }
    // $rate = round($count/(count($sentencesArr)),4)*100;
    echo '</table>';
    
